﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;
using System.Text;

namespace WebApp
{
    public partial class RoleModuleFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(RoleModuleFrm), this);
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<string[]> GetMenus(int roleId)
        {
            AjaxResult<string[]> result = new AjaxResult<string[]>();
            result.Data = null;
            result.Msg = null;
            try
            {
                StringBuilder strRight = new StringBuilder();
                StringBuilder strLeft = new StringBuilder();
                IEnumerable<TB_RoleModule> roleModules = this.BaseDataProxy.FindRoleModules(roleId);
                IEnumerable<TB_SysModule> menus = this.BaseDataProxy.FindSysModules(null, null);
                if (menus != null)
                {
                    foreach (TB_SysModule m in menus)
                    {
                        if (roleModules != null && roleModules.Any(p => p.moduleId == m.id))
                            strRight.AppendLine(string.Format("<option value=\"{0}\">{1}</option>", m.id, m.moduleName));
                        else
                            strLeft.AppendLine(string.Format("<option value=\"{0}\">{1}</option>", m.id, m.moduleName));
                    }
                }
                result.Data = new string[] { strLeft.ToString(), strRight.ToString() };
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> Save(int roleId, string[] items)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Save, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                List<TB_RoleModule> datas = null;
                if (items != null)
                {
                    datas = new List<TB_RoleModule>();
                    foreach (var i in items)
                    {
                        TB_RoleModule rm = new TB_RoleModule();
                        rm.roleId = roleId;
                        rm.moduleId = Convert.ToInt32(i);
                        datas.Add(rm);
                    }
                }
                result.Data = this.BaseDataProxy.UpdateRoleModule(roleId, datas.ToArray());
                if (!result.Data)
                    result.Msg = "保存异常！";
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
